package zjc.bookkeeping.tools;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import java.util.List;

import zjc.bookkeeping.R;
import zjc.bookkeeping.adapter.MethodListAdapter;
import zjc.bookkeeping.db.DBManager;
import zjc.bookkeeping.db.MethodBean;

public class MethodDialog extends Dialog {
    String method;
    int id;
    ListView listView;
    List<MethodBean> methodBeanList = DBManager.getAllMethod();
    MethodListAdapter methodListAdapter;
    public interface OnMethodItemListener{
        public void onMethodItemClick();
    }
    OnMethodItemListener onMethodItemListener;

    public void setOnMethodItemListener(OnMethodItemListener onMethodItemListener) {
        this.onMethodItemListener = onMethodItemListener;
    }

    public MethodDialog(@NonNull Context context) {
        super(context);
    }

    public String getMethod() {
        return method;
    }
    public int getMethodId(){
        return id;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_method);
        Window window = getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        loadListView();
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        getWindow().setAttributes(layoutParams);
    }

    private void loadListView() {
        listView = findViewById(R.id.dialog_method_lv);
        if (methodBeanList.size() == 0){
            MethodBean methodBean = new MethodBean();
            methodBean.setMethod_name("无账户");
            methodBeanList.add(methodBean);
            Log.i("名字",methodBean.getMethod_name()+"图片："+methodBean.getMethod_image()+"金额："+methodBean.getMethod_money());
        }
        methodListAdapter = new MethodListAdapter(getContext(),methodBeanList);
        listView.setAdapter(methodListAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                TextView textView = view.findViewById(R.id.dialog_method_item_method);
                method = textView.getText().toString();
                id = methodBeanList.get(i).getMethod_id();
                onMethodItemListener.onMethodItemClick();
                cancel();
            }
        });
    }
}
